
class Animal{
    String name;
    int age;
    public Animal(String name,int age){
        this.name=name;
        this.age=age;
    }
    public void eat(){
        System.out.println(name+"正在吃饭！");
    }
}

class Dog extends Animal{
    public Dog(String name,int age){
        super(name,age);
    }
    public void eat(){//构成重写
        System.out.println(name+"正在吃骨头！");
    }
}

class Cat extends Animal{
    public Cat(String name,int age){
        super(name,age);
    }
    public void eat(){//构成重写
        System.out.println(name+"正在吃鱼！");
    }
}

public class TestAnimal {
    public static void eat(Animal a){
        a.eat();
    }

    public static Animal byAnimal(String name){
        if("狗".equals(name)){
            return new Dog("狗狗",1);
        }else if("猫".equals(name)){
            return new Cat("喵喵",1);
        }else{
            return null;
        }
    }

    public static void main(String[] args) {
        //1、直接赋值
        Animal cat=new Cat("小橘",3);
        Animal dog=new Dog("小黄",3);
        //2、方法传参
        eat(new Cat("小橘",3));
        eat(new Dog("小黄",3));
        //3、方法返回
        Animal dog_=byAnimal("狗");
        Animal cat_=byAnimal("猫");
    }
}
